<?php
  

   // 封装函数：提取时有默认值和没有默认值
  function getpages($link,$table,$per_page,$cur_page=1,$page_num=5){
    //固定每页显示的记录数

   //数据表的总记录
   $sql="select count(*) as total from `$table` ";
   
   $result = mysqli_query($link,$sql);

   $total_num = mysqli_fetch_assoc($result)['total'];

   // print_r($total_num);die;

   $total_page=ceil($total_num/$per_page);

   //访问a标签页码的前后偏移量,向下取整

   $offset_page = floor($page_num/2);
    //开始页码，当前页码-前后偏移幅

    $start = $cur_page-$offset_page;
        //结束页码
     $end = $cur_page+$offset_page;
   
  

     if($end>$total_page){
      $start=$total_page-$page_num+1;
      $end=$total_page;
     }
  if($start<1){
         $start=1;
         $end=$page_num;
     }

 

    $pages = '';

        
     $disabled = $cur_page == 1?'disabled':''; 

     $href = $cur_page==1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p=1';

    //首页
    $pages .='<li class="'.$disabled.'"><a href="'.$href.'">首页</a></li>';

    //上页

    $prev = $cur_page-1;

    $prev_href = $cur_page== 1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$prev;

     $pages .='<li class="'.$disabled.'"><a  href="'.$prev_href.'">上页</a></li>';

   

        for($i=$start;$i<=$end;$i++){
             if($i==$cur_page){ 
            
           $pages .= '<li class="pcur"><a>'.$i.'</a></li>';
             
             } else {
            $pages .='<li><a href="index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$i.' ">'.$i.'</a></li>';
             } 
             }
   

       $next = $cur_page+1;

         $next_href = ($cur_page==$total_page)?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$next; 

        $display = $cur_page == $total_page?'disabled':'';  
             
        $pages .='<li class="'.$display.'"><a href="'.$next_href.'">下页</a></li>';

   
         $last_href = $cur_page==$total_page?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p=' .$total_page;
        $pages .='<li class="'.$display.'"><a href="'.$last_href.'">尾页</a></li>';
             

             return $pages;


}





















































